package com.jhws.business.feedback.api;

import com.alibaba.fastjson.JSON;
import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.feedback.bo.FeedbackConstant;
import com.jhws.business.feedback.bo.FeedbackInfo;
import com.jhws.business.feedback.service.FeedbackService;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/v2/feedback")
public class FeedbackApiV2 {
    @Resource
    private FeedbackService feedbackService;
    @Resource
    private ITblOwnerDimService ownerDimService;
    @Resource
    private OSSClientService ossClientService;

    /**
     * 保存反馈信息
     */
    @RequestMapping("saveFeedback")
    @ResponseBody
    public JsonRespData saveFeedback(FeedbackInfo feedbackInfo) throws Exception {
        List<TblOwnerDimBo> list = ownerDimService.findTblOwnerDimByWhere("where owner_mob = '" + feedbackInfo.getPhone() + "'");
        if (list != null && list.size() > 0) {
            TblOwnerDimBo ownerDimBo = list.get(0);
            feedbackInfo.setState(FeedbackConstant.FEEDBACK_STATE_NOT_HANDLED);
            feedbackInfo.setUser(ownerDimBo.getOwnerCode());
            feedbackInfo.setUserName(ownerDimBo.getOwnerName());
            feedbackInfo.setCommCode(ownerDimBo.getCommCode());
        }

        List<String> urls = new ArrayList<>();
        List<String> tempUrls = JSON.parseArray(feedbackInfo.getUrls(), String.class);
        if (tempUrls != null) {
            for (String url : tempUrls) {
                String fileName = FilenameUtils.getName(url);
                String destKey = "cms/file/" + fileName;
                ossClientService.gerneralCopyFile(url, destKey);
                urls.add(destKey);
            }
        }
        feedbackInfo.setType(FeedbackConstant.FEED_TYPE_APP_USER);
        feedbackInfo.setUrls(JSON.toJSONString(urls));
        feedbackService.saveFeedback(feedbackInfo);
        return new JsonRespData("", 1, "成功");
    }

    /**
     * 根据手机号码获取自己的反馈信息
     *
     * @param phone
     * @return
     */
    @RequestMapping("queryFeedback")
    @ResponseBody
    public JsonRespData queryFeedback(String phone) {
        String where = "where owner_mob='" + phone + "' and type='" + FeedbackConstant.FEED_TYPE_APP_USER + "'";
        return new JsonRespData("", 1, feedbackService.queryFeedbackInfoByWhere(where));
    }
}
